/**
 * 核心技术支持
 * IT渠道网 小程序商城
 * 13873174181 微信同号
 */
import {List} from '../list/list-model.js';
var list = new List();

Page({
    data: {
        sort: 0,
        sort_type: -1,
        showLoadingBar: false,
        isSales: false,
        isShow: false,
        cats: ['默认', '商品'],
        keyword: null,
        dfCat: 0,
        search: {keywords: null, pageNo: 1, pageSize: 6, category: "", brand: "", spec: {}, price: "", sortField: "", sort: "", platform: "mob"},
        totalPage: 0,
        goodsList: [],
        loadingHidden: false,
        restUrl: list.getUrl(),
        isLoadedAll: false,
        showData: false
    },
    onLoad: function(option) {
        var keyword = option.keyword;
        this.data.search.keywords = keyword;
        this.setData({
            keyword: keyword,
            search: this.data.search
        })
        this.loadData();
    },
    /**
     * 查询数据
     */
    loadData: function(callback) {
        var that = this;
        list.getList(that.data.search, (data)=>{
            //console.log(0, that.data.search)
            //console.log(1, data)
            if (data.total == undefined || data.total == 0) {
                that.setData({
                    loadingHidden: true,
                    showData: true
                })
                return;
            }
            // 数据处理
            var list = data.rows;
            for (let i in list) {
                list[i].title = list[i].title.replace("<em style='color:red'>", "").replace("</em>", "").substring(0, 13);
            }
            // 上拉触底函事件,追加到[]中,显示追加效果,数组合并
            that.data.goodsList.push.apply(that.data.goodsList, list);
            that.setData({
                goodsList: that.data.goodsList,
                totalPage: data.totalPages,
                loadingHidden: true
            })
            callback && callback();
        });
        // 存储到搜索历史
        this.setHistory(this.data.search.keywords);
    },
    /**
     * 监听用户下拉动作 重新刷新用户收藏,回到&page=1
     */
    onPullDownRefresh: function() {
        var that = this,
            search = that.data.search;
        search.pageNo = 1;
        that.data.search = search;
        that.data.goodsList = [];  // 初始化
        that.loadData(() => {
            that.setData({
                isLoadedAll: false
            });
            wx.stopPullDownRefresh()
        })
    },
    /**
     * 页面上拉触底事件的处理函数 上拉到底部触发&page = pageNo + 1
     */
    onReachBottom: function() {
        var search = this.data.search;
        if (!this.data.isLoadedAll && search.pageNo < this.data.totalPage) {
            search.pageNo = search.pageNo + 1;
            this.data.search = search;
            this.loadData();
        } else if (search.pageNo == this.data.totalPage){
            this.setData({
                isLoadedAll: true
            })
        }
    },
    /**
     * 导航切换
     */
    sortClick: function(t) {
        var a = this,
            e = t.currentTarget.dataset.sort, // sort 0 1 2 3
            i = (null == t.currentTarget.dataset.default_sort_type) ? -1 : t.currentTarget.dataset.default_sort_type, // sort_type 0 1
            o = a.data.sort_type;
        if (a.data.sort == e) {
            if (-1 == i) return;
            o = (-1 == o) ? i : (0 == o ? 1 : 0);
        } else o = i;
        // 排序
        var search = a.data.search,
            sortField = search.sortField,
            sort = search.sort;
            search.pageNo = 1;
        if (e == 2) {
            sortField = 'prices';
            if (o == 0) {
                sort = 'ASC'
            } else sort = 'DESC';
        } else if (e == 1 || e == 3) {
            sortField = 'updatetime';
            if (sort == 'DESC' || sort == '') {
                 sort = 'ASC'
            } else sort = 'DESC';
        } else {
            sortField = '';
            sort = '';
        }
        // 更新数据
        search.sortField = sortField;
        search.sort = sort;
        a.setData({
            sort: e,
            sort_type: o,
            search: search,
            goodsList: [],
            isLoadedAll: false
        });
        // 追加数据
        a.loadData()
    },
    pullDown: function() {
        this.setData({
            isShow: !this.data.isShow
        })
    },
    changeCat: function(e) {
        var i = e.currentTarget.dataset.index;
        this.setData({
            dfCat: i
        })
    },
    /**
     * 输入框聚焦时
     */
    inputFocus: function() {
    },
    /**
     * 失去焦点, 保存到历史
     */
    inputBlur: function(e) {
        var value = e.detail.value;
        this.setData({
            keyword: value
        })
    },
    /**
     * 点击完成按钮, Enter/手机键盘
     */
    inputConfirm: function(e) {
        var value = e.detail.value;
        this.searchCancel(value, 1);
    },
    /**
     * 搜索
     */
    searchCancel: function(value, t) {
        var keyword;
        if (t == 1) {
            keyword = value
        } else {
            keyword = this.data.keyword
        }
        if (keyword == null || keyword == '') {
            wx.showToast({
                title: '请输入关键词',
                icon: 'none',
                duration: 1000
            })
            return
        }
        this.data.search = {keywords: keyword, pageNo: 1, pageSize: 6, category: "", brand: "", spec: {}, price: "", sortField: "", sort: "", platform: "mob"};
        this.setData({
            sort: 0,
            loadingHidden: false,
            isLoadedAll: false,
            goodsList: []
        })
        this.loadData()
    },
    /**
     * 搜索历史
     */
    getHistoryList: function() {
        var a = wx.getStorageSync('historySearch');
        if (!a) return [];
        return a;
    },
    setHistory: function(value) {
        var a = this.getHistoryList();
        // 校验是否存在
        if (a.length > 0) {
            for (let i in a) {
                if (a[i].keyword == value) return;
            }
        };
        a.unshift({'keyword': value});
        if (a.length > 20) {
            a.splice(a.length - 1, 1);
        };
        wx.setStorageSync('historySearch', a);
    }
})